package com.company.genealogy.web.controller;

import com.company.genealogy.common.result.ApiResponse;
import com.company.genealogy.web.entity.Region;
import com.company.genealogy.web.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import com.company.genealogy.common.constant.ApiCodeEnum;

@RestController
@RequestMapping("/api/v1/regions")
public class RegionController {
    
    @Autowired
    private RegionService regionService;
    
    /**
     * 获取省份列表
     */
    @GetMapping("/provinces")
    public ApiResponse<List<Region>> getProvinces() {
        List<Region> provinces = regionService.getProvinces();
        return ApiResponse.success(ApiCodeEnum.SUCCESS, provinces);
    }
    
    /**
     * 获取城市列表
     */
    @GetMapping("/cities")
    public ApiResponse<List<Region>> getCities(@RequestParam String provinceCode) {
        List<Region> cities = regionService.getCities(provinceCode);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, cities);
    }
    
    /**
     * 获取区县列表
     */
    @GetMapping("/counties")
    public ApiResponse<List<Region>> getCounties(@RequestParam String cityCode) {
        List<Region> counties = regionService.getCounties(cityCode);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, counties);
    }
    
    /**
     * 获取镇/街道列表
     */
    @GetMapping("/towns")
    public ApiResponse<List<Region>> getTowns(@RequestParam String countyCode) {
        List<Region> towns = regionService.getTowns(countyCode);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, towns);
    }
    
    /**
     * 根据编码获取地区信息
     */
    @GetMapping("/{code}")
    public ApiResponse<Region> getRegionByCode(@PathVariable String code) {
        Region tb_region = regionService.getRegionByCode(code);
        return ApiResponse.success(ApiCodeEnum.SUCCESS, tb_region);
    }
    
    /**
     * 获取地区树形结构
     */
    @GetMapping("/tree")
    public ApiResponse<List<Region>> getRegionTree() {
        List<Region> regionTree = regionService.getRegionTree();
        return ApiResponse.success(ApiCodeEnum.SUCCESS, regionTree);
    }
} 